package com.example.demo;

import com.example.demo.websocket.WebSocketClient;
import redis.clients.jedis.Jedis;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.net.URL;

public class Swing extends JFrame {


    /**
     * @param args
     */
    public static void main(String[] args) {
        //Jedis jedis = new Jedis("47.98.229.220", 6379);
        //Swing swing = new Swing();
        //swing.create(jedis);
        try {
            WebSocketClient.startSocket();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void create(Jedis jedis) {
        // TODO Auto-generated method stub
        this.setLayout(new BorderLayout());
        //is.setLayout(new GridLayout(2,2,10,5)); //默认为1行，n列；2行3列，水平间距10，垂直间距5
        //this.setLayout(new GridLayout(2, 2, 10, 30));
        this.setUndecorated(true);
        //Border b = new CompoundBorder(new EtchedBorder(), new LineBorder(Color.RED));
        JPanel panel = new JPanel();
        panel.setSize(100, 70);
        //panel.setBorder(b);：
        JLabel z0 = new JLabel("<html>初始化中...<html>");
        panel.add(z0);

//输入框取出，待开发
//        JTextField username = new JTextField();
//        username.setColumns(10);
        //this.add(username, BorderLayout.NORTH);

        new Timer(1000, new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String hello = jedis.get("hello");
                z0.setText(hello);
            }
        }).start();

        //鼠标拖动监听
        mouseMotionListener(panel, this);
        this.setLocation(0, 0);
        this.getContentPane().add(panel);
        this.setSize(100, 70);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setAlwaysOnTop(true);
        this.show();
        addSystemTray(this);
    }

    public static void mouseMotionListener(JPanel panel, Frame frame) {
        panel.addMouseMotionListener(new MouseAdapter() {
            private boolean top = false;
            private boolean down = false;
            private boolean left = false;
            private boolean right = false;
            private boolean drag = false;
            private Point lastPoint = null;
            private Point draggingAnchor = null;

            @Override
            public void mouseMoved(MouseEvent e) {
                if (e.getPoint().getY() == 0) {
                    frame.setCursor(Cursor.getPredefinedCursor(Cursor.N_RESIZE_CURSOR));
                    top = true;
                } else if (Math.abs(e.getPoint().getY() - frame.getSize().getHeight()) <= 1) {
                    frame.setCursor(Cursor.getPredefinedCursor(Cursor.N_RESIZE_CURSOR));
                    down = true;
                } else if (e.getPoint().getX() == 0) {
                    frame.setCursor(Cursor.getPredefinedCursor(Cursor.E_RESIZE_CURSOR));
                    left = true;
                } else if (Math.abs(e.getPoint().getX() - frame.getSize().getWidth()) <= 1) {
                    frame.setCursor(Cursor.getPredefinedCursor(Cursor.E_RESIZE_CURSOR));
                    right = true;
                } else {
                    frame.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
                    draggingAnchor = new Point(e.getX() + panel.getX(), e.getY() + panel.getY());
                    top = false;
                    down = false;
                    left = false;
                    right = false;
                    drag = true;
                }

            }

            @Override
            public void mouseDragged(MouseEvent e) {
                Dimension dimension = frame.getSize();
                if (top) {

                    dimension.setSize(dimension.getWidth(), dimension.getHeight() - e.getY());
                    frame.setSize(dimension);
                    frame.setLocation(frame.getLocationOnScreen().x, frame.getLocationOnScreen().y + e.getY());
                } else if (down) {

                    dimension.setSize(dimension.getWidth(), e.getY());
                    frame.setSize(dimension);

                } else if (left) {

                    dimension.setSize(dimension.getWidth() - e.getX(), dimension.getHeight());
                    frame.setSize(dimension);


                    frame.setLocation(frame.getLocationOnScreen().x + e.getX(), frame.getLocationOnScreen().y);

                } else if (right) {

                    dimension.setSize(e.getX(), dimension.getHeight());
                    frame.setSize(dimension);
                } else {
                    frame.setLocation(e.getLocationOnScreen().x - draggingAnchor.x, e.getLocationOnScreen().y - draggingAnchor.y);
                }
            }
        });

    }

    public static void addSystemTray(Frame frame) {
        /*
         * 添加系统托盘
         */
        if (SystemTray.isSupported()) {
            // 获取当前平台的系统托盘
            SystemTray tray = SystemTray.getSystemTray();

            // 加载一个图片用于托盘图标的显示
//            Image image = Toolkit.getDefaultToolkit().getImage("/pic.jpg");
//
            URL imgUrl = Swing.class.getClassLoader().getResource("pic.jpg");
////            // 通过getImage()函数获取图片
            Image image = Toolkit.getDefaultToolkit().getImage(imgUrl);


            // 创建点击图标时的弹出菜单
            PopupMenu popupMenu = new PopupMenu();

            MenuItem openItem = new MenuItem("打开");
            MenuItem exitItem = new MenuItem("退出");

            openItem.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    // 点击打开菜单时显示窗口
                    if (!frame.isShowing()) {
                        frame.setVisible(true);
                    }
                }
            });
            exitItem.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    // 点击退出菜单时退出程序
                    System.exit(0);
                }
            });

            popupMenu.add(openItem);
            popupMenu.add(exitItem);

            // 创建一个托盘图标
            TrayIcon trayIcon = new TrayIcon(image, "这是一个托盘图标", popupMenu);

            // 托盘图标自适应尺寸
            trayIcon.setImageAutoSize(true);

            trayIcon.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    System.out.println("托盘图标被右键点击");
                }
            });
            trayIcon.addMouseListener(new MouseAdapter() {
                @Override
                public void mouseClicked(MouseEvent e) {
                    switch (e.getButton()) {
                        case MouseEvent.BUTTON1: {
                            System.out.println("托盘图标被鼠标左键被点击");
                            break;
                        }
                        case MouseEvent.BUTTON2: {
                            System.out.println("托盘图标被鼠标中键被点击");
                            break;
                        }
                        case MouseEvent.BUTTON3: {
                            System.out.println("托盘图标被鼠标右键被点击");
                            break;
                        }
                        default: {
                            break;
                        }
                    }
                }
            });

            // 添加托盘图标到系统托盘
            try {
                tray.add(trayIcon);
            } catch (AWTException e) {
                e.printStackTrace();
            }

        } else {
            System.out.println("当前系统不支持系统托盘");
        }
    }


}